package controller;

import globals.SystemGeneralState;
import gui.mainframe.MainFrame;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

public class GlobalOperations {
	/*
	 * logger
	 */
	private final static Logger logger = Logger.getLogger(MainFrame.class);
	
	static SystemGeneralState sgs;
	
	public GlobalOperations() {}
	
	public void setGlobal(String key, int value) {
		sgs = SystemGeneralState.getInstance();
		sgs.put(key, String.valueOf(value));
		logger.info("Storing int global - key '" + key + "', value: " + value);
	}
	
	public void setGlobal(String key, String value) {
		sgs = SystemGeneralState.getInstance();
		sgs.put(key, value);
		logger.info("Storing String global - key '" + key + "', value: " + value);
	}
	
	public int getIntFromGlobal(String str) {
		int i = -1;
		sgs = SystemGeneralState.getInstance();
		logger.info("Retrieving int global - key '" + str + "', value: " + sgs.get(str));
		i = (sgs.get(str) == null) ? -1 : Integer.parseInt(sgs.get(str));
		return i;
	}

	public String getStringFromGlobal(String str) {
		String s = "";
		sgs = SystemGeneralState.getInstance();
		logger.info("Retrieving String global - key '" + str + "', value: '" + sgs.get(str) + "'");
		s = (sgs.get(str) == null) ? "" : sgs.get(str);
		return s;
	}
	
	public Properties getLoggerProperties() {
		Properties props = new Properties();
		
		try {
			props.load(new FileInputStream("C:/Pasarelle/logs/log4j.properties"));
			System.out.println("using props from C:/Pasarelle/logs/log4j.properties:\n" + props.toString());
		} catch (IOException e) {
			System.err.println("IOException finding C:/Pasarelle/logs/log4j.properties");
		}
		
		if(props == null || props.isEmpty()) {
			try {
				props.load(new FileInputStream("/home/alx/log4j/log4j.properties"));
				System.out.println("using props from /home/alx/log4j/log4j.properties:\n" + props.toString());
			} catch (IOException e) {
				System.err.println("IOException finding /home/alx/log4j/log4j.properties");
			}
		}
		
		return props;
	}
	
//	public void setGlobalInteger(String key, int value) {
//		sgs = SystemGeneralState.getInstance();
//		sgs.put(key, value);
//	}
}
